<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        body {
            margin: 0;
        }
        div {
            width: 100px;
            height: 100px;
            background-color: orange;
            position: absolute;
            left: 1000px;
            top: 0;
        }
        button {
            margin-top: 150px;
        }
        span {
            position: absolute;
            left: 600px;
            top: 0;
            width: 1px;
            height: 100px;
            background: #000;
        }
        /* css设置的时候像素值不要给小数，因为视觉上感受不到，而且计算机对小数渲染的处理也不是很精准 */
    </style>
</head>
<body>
    <div></div>
    <span></span>
    <button id="btn">run</button>
    <script>
        // div.offsetLeft 返回的是整数
        // 缓冲运动，物体的速度，距离目标点越近，就越小，到达目标点时，速度减小为0
        // js div 
        var div = document.getElementsByTagName('div')[0];
        var btn = document.getElementById('btn');
        var timer = null;
        btn.onclick = function() {
            startMove(div, 600);
        }

        function startMove(dom, target) {
            clearInterval(timer);
            dom.style.left = window.getComputedStyle(dom, null).left;
            var speed = null;
            var num = 0;
            timer = setInterval(function(){
                speed = (target - parseInt(dom.style.left)) / 7;
                speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
                // 负数的向上取整是 -1.9 --> -1
                // Math.floor(-1.9) --> -2
                if(parseInt(dom.style.left) == target) {
                    clearInterval(timer);
                }else {
                    num = parseInt(dom.style.left) + speed;
                    // console.log(num);
                    dom.style.left = num  + 'px'; 
                }
            }, 30);
        }


    </script>
</body>
</html>